package com.ziyun.pop.web.modules.storeMaterials.controller;

import com.utility.New;
import com.utility.StringUtils;
import com.utility.http.HttpUtils;
import com.ziyun.pop.common.e.material.MaterialStatusEnum;
import com.ziyun.pop.common.e.material.MaterialUnitEnum;
import com.ziyun.pop.common.utils.JsonUtils;
import com.ziyun.pop.common.utils.UserUtils;
import com.ziyun.pop.modules.storeMaterials.entity.MaterialEntity;
import com.ziyun.pop.modules.storeMaterials.service.ErpStockService;
import com.ziyun.pop.modules.storeMaterials.service.MaterialService;
import com.ziyun.pop.web.modules.capital.controller.QueryPage;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.utils.StringUtil;
import io.renren.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author ZhaoWei
 * @Date 2018/10/18 14:10
 * @Version V1.0
 */
@RestController
@RequestMapping("material/material")
public class MaterialController {

	@Autowired
	private MaterialService materialService;

	@Autowired
	private ErpStockService erpStockService;

	@Value("${erpApi}")
	private String erpApi ;

	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params) {
		Long sellerId = UserUtils.getCurentUser().getSellerId();
		params.put("sellerId",sellerId);
		PageUtils page = materialService.queryPage(params);

		R rs = R.ok();
		rs.put("page",page);

		// 存放枚举类到userdate

		//存放枚举类到userdata
		Map<String, List> mapEnum = new HashMap<>();
		mapEnum.put("materialUnitEnum", JsonUtils.enumToList(MaterialUnitEnum.class));
		mapEnum.put("materialStatusEnum", JsonUtils.enumToList(MaterialStatusEnum.class));

		rs.put("userData", mapEnum);

		return rs;

	}

	// 信息获取
	@RequestMapping("/info/{materialId}")
	public R info(@PathVariable("materialId") Integer materialId){
		MaterialEntity material = materialService.selectById(materialId);

		return R.ok().put("material", material);
	}


	// 信息修改保存
	@RequestMapping("/update")
	public R update(@RequestBody MaterialEntity material){
		ValidatorUtils.validateEntity(material);
		Map<String,Object> map = new HashMap<>();
		map.put("MaterialId",material.getMaterialId());
		map.put("MaterialQuantity",material.getMaterialQuantity());
		map.put("MaterialCode",material.getMaterialCode());
		map.put("MaterialName",material.getMaterialName());
		map.put("CategoryId",material.getCategoryId());
		map.put("Status",material.getStatus());
		map.put("Unit",material.getUnit());
		map.put("MaterialModel",material.getMaterialModel());
		map.put("Specifications",material.getSpecifications());
		map.put("Length",material.getLength());
		map.put("Width",material.getWidth());
		map.put("Weight",material.getWeight());
		map.put("AuditName",material.getAuditName());
		map.put("AuditTime",material.getAuditTime());
		map.put("UpdateName",UserUtils.getCurentUser().getUsername());
		map.put("CreateName",material.getCreateName());
		map.put("Cost",material.getCost());
		materialService.updateMaterialAllColumnById(map);//全部更新
		return R.ok();
	}

	/**
	 * 列表
	 */
	@RequestMapping("/queryMaterialsList")
	public R queryMaterialsList(@RequestBody QueryPage query) {
//		materialService.queryMaterialsListPage(query.getPage(), query.getParams());
		String url = erpApi + "/material/materialcategory/queryMaterialsList";
		Map<String, Object> paramHeaderMap = New.hashMap();
		Map<String, Object> paramBodyMap = New.hashMap();
		paramBodyMap.put("categoryId",query.getParams().get("categoryId"));
		String response = HttpUtils.doPost(url, paramHeaderMap, com.utility.JsonUtils.map2String(paramBodyMap));
		if(StringUtils.isNotBlank(response)){
			Map<String, Object> resultMap = com.ziyun.pop.common.utils.JsonUtils.jsonToMap(response);
			return  R.ok().put("result",resultMap);
		}else{
			return  R.error().put("msg","接口调用异常");
		}

//		return R.ok().put("page", query.getPage());
	}

}


